using System;
using CPlatePrintSys.Domain.Models;
using Prism.Mvvm;

namespace CPlatePrintSys.Win.ViewModels
{
    /// <summary>
    /// PlateAnnotationInfo的UI绑定ViewModel
    /// </summary>
    public class PlateAnnotationInfoViewModel : BindableBase
    {
        private readonly PlateAnnotationInfo _plateAnnotationInfo;
        private bool _isUpdatingSelection = false; // 防止循环更新的标志

        public PlateAnnotationInfoViewModel(PlateAnnotationInfo plateAnnotationInfo)
        {
            _plateAnnotationInfo = plateAnnotationInfo ?? throw new ArgumentNullException(nameof(plateAnnotationInfo));
        }

        /// <summary>
        /// 是否被选中（用于UI多选）
        /// </summary>
        private bool _isSelected;
        public bool IsSelected
        {
            get => _isSelected;
            set 
            {
                if (SetProperty(ref _isSelected, value))
                {
                    // 防止循环更新
                    if (_isUpdatingSelection)
                        return;

                    _isUpdatingSelection = true;
                    try
                    {
                        // 当选择状态改变时，触发全选状态更新
                        OnSelectionChanged();
                    }
                    finally
                    {
                        _isUpdatingSelection = false;
                    }
                }
            }
        }

        /// <summary>
        /// 选择状态改变事件
        /// </summary>
        public event Action SelectionChanged;

        /// <summary>
        /// 触发选择状态改变事件
        /// </summary>
        private void OnSelectionChanged()
        {
            SelectionChanged?.Invoke();
        }

        /// <summary>
        /// 获取原始的PlateAnnotationInfo对象
        /// </summary>
        public PlateAnnotationInfo OriginalData => _plateAnnotationInfo;

        #region 数据属性（从原始对象映射）

        public int Id => _plateAnnotationInfo.Id;
        public string MotherShipNo => _plateAnnotationInfo.MotherShipNo;
        public string StreamShipNo => _plateAnnotationInfo.StreamShipNo;
        public string LotNo => _plateAnnotationInfo.LotNo;
        public string PlateNo => _plateAnnotationInfo.PlateNo;
        public string Sequence => _plateAnnotationInfo.Sequence;
        public string CNO => _plateAnnotationInfo.CNO;
        public string DesignSpecCls => _plateAnnotationInfo.DesignSpecCls;
        public double DesignSpecThickness => _plateAnnotationInfo.DesignSpecThickness;
        public double DesignSpecLength => _plateAnnotationInfo.DesignSpecLength;
        public double DesignSpecWidth => _plateAnnotationInfo.DesignSpecWidth;
        public string SheetConsistentNo => _plateAnnotationInfo.SheetConsistentNo;
        public string SheetSpecCls => _plateAnnotationInfo.SheetSpecCls;
        public double SheetSpecThickness => _plateAnnotationInfo.SheetSpecThickness;
        public double SheetSpecLength => _plateAnnotationInfo.SheetSpecLength;
        public double SheetSpecWidth => _plateAnnotationInfo.SheetSpecWidth;
        public LayoutMean LayoutMean => _plateAnnotationInfo.LayoutMean;
        public string MergedInfo => _plateAnnotationInfo.MergedInfo;
        public string LayoutInfo => _plateAnnotationInfo.LayoutInfo;
        public DateTime? PlanUsedDate => _plateAnnotationInfo.PlanUsedDate;
        public bool IsAnnotationGenerated => _plateAnnotationInfo.IsAnnotationGenerated;
        public string DesignDrawingPath => _plateAnnotationInfo.DesignDrawingPath;
        public string AnnotationDrawingPath => _plateAnnotationInfo.AnnotationDrawingPath;
        public SourceType SourceType => _plateAnnotationInfo.SourceType;

        #endregion

        /// <summary>
        /// 从PlateAnnotationInfo列表创建ViewModel列表
        /// </summary>
        public static System.Collections.ObjectModel.ObservableCollection<PlateAnnotationInfoViewModel> CreateFromList(
            System.Collections.Generic.IEnumerable<PlateAnnotationInfo> plateAnnotationInfos)
        {
            var viewModels = new System.Collections.ObjectModel.ObservableCollection<PlateAnnotationInfoViewModel>();
            int index = 0;
            foreach (var info in plateAnnotationInfos)
            {
                info.Id = ++index;
                viewModels.Add(new PlateAnnotationInfoViewModel(info));
            }
            return viewModels;
        }

        /// <summary>
        /// 从ViewModel列表获取选中的原始数据
        /// </summary>
        public static System.Collections.Generic.List<PlateAnnotationInfo> GetSelectedOriginalData(
            System.Collections.Generic.IEnumerable<PlateAnnotationInfoViewModel> viewModels)
        {
            var selectedData = new System.Collections.Generic.List<PlateAnnotationInfo>();
            foreach (var vm in viewModels)
            {
                if (vm.IsSelected)
                {
                    selectedData.Add(vm.OriginalData);
                }
            }
            return selectedData;
        }

        /// <summary>
        /// 刷新指定属性的UI通知
        /// </summary>
        public void RefreshProperty(string propertyName)
        {
            RaisePropertyChanged(propertyName);
        }
    }
} 